﻿using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WenSkin.Forms
{
    public partial class LodingForm : Form
    {
        public LodingForm()
        {
            InitializeComponent();
            wenImageButton1.Visible = false;
        }

        public string Mes
        {
            get => label1.Text;
            set
            {
                if (this.IsHandleCreated)
                {
                    this.Invoke(new Action(() =>
                    {
                        label1.Text = value;
                    }));
                }
                else
                    label1.Text = value;
            }
        }

        public int Value = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Value > 100)
            {
                Value = 0;
            }
            Value += 10;

            wenCircleProgress1.Value = Value;
        }
        /// <summary>
        /// 开启新的线程启动
        /// </summary>
        public void Start()
        {
            new Task(() =>
            {
                this.ShowDialog();
            }).RunSynchronously();
        }

        /// <summary>
        /// 不用管理线程关闭
        /// </summary>
        public void End()
        {
            AcColse();
        }

        public void AcColse()
        {
            Invoke(new Action(() =>
            {
                this.Close();
            }));
        }

        public bool PEnabled
        {
            get => timer1.Enabled;
            set
            {
                timer1.Enabled = value;
                if (!value)
                {
                    Action action = () =>
                    {
                        wenImageButton1.Visible = true;
                        wenCircleProgress1.Value = 100;
                    };
                    Invoke(action);
                }
            }
        }
        public void MessageClick(string mes, bool enabled)
        {
            Mes = mes;
            PEnabled = enabled;
        }

        private void wenImageButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
